<?php
namespace app\applet\controller;

use app\BaseController;
use http\Client;
use think\facade\Cache;
use think\facade\Db;
use think\facade\View;


class Index extends BaseController
{
    private $dlbz = [
        'url'=>'http://www.zg-gi.com/apis/Product/',
        'username'=>'yunnansy',
        'kenCode'=>'b3ef9aaa468545c49003c6ab27740f39',
        'time'=>3600
    ];
    public function index()
    {

        $traceability = 'traceability';
        $code = input('get.c');//oYRX3AO1Oysrv6lxJvYo7CICWq3wuCVc--//nGopaOchFZMrv6lxJvYo7CICWq3wuCVc
        $code = openssl_decrypt($code, 'DES-ECB', config('app')['cache_shaf'], 0);
        $traceability_info =  Db::name($traceability)
            ->where('code',$code)
            ->find();
        if(!$traceability_info){
            return  json_encode(re_error('您查询的'.$code.'产品不存在'));
            die();
        }

        //批次信息
        $batch_info =  Db::name('batch')
            ->where('wdm_open','<',$traceability_info['wdm'])
            ->where('wdm_end','>',$traceability_info['wdm'])
            ->whereOr('wdm_open','=',$traceability_info['wdm'])
            ->whereOr('wdm_end','=',$traceability_info['wdm'])
            ->find();

        if(!$batch_info){
            return  json_encode(re_error('空余的编码'));
            die();
        }

        //单位信息
        $company_info =  Db::name('company')
            ->where('id',$batch_info['company_id'])
            ->where('status',1)
            ->find();
        unset($company_info['password']);
        unset($company_info['key']);

        //产品信息
        $product_info =  Db::name('product')
            ->where('id',$batch_info['product_id'])
            ->find();
        if($product_info){
            $product_info['image'] = $product_info['image']?json_decode($product_info['image']):[];
            $product_info['images'] = $product_info['images']?json_decode($product_info['images']):[];
            $product_info['protection_image'] = $product_info['protection_image']?json_decode($product_info['protection_image']):[];

        }


        //批次等级
        $grade_info =  Db::name('grade')
            ->where('id',$batch_info['grade_id'])
            ->where('status',1)
            ->find();


        $jball = ['一星级','二星级','三星级','四星级','五星级'];
        $grade_info['rank'] = $jball[intval($grade_info['rank'])-1];
        if($grade_info){
            $grade_info['image'] = $grade_info['image']?json_decode($grade_info['image']):[];
        }

        //批次检测检验
        $proof_list =  Db::name('proof')
            ->where('batch_id',$batch_info['id'])
            ->where('status',1)
            ->select()->toArray();
        if($proof_list){
            foreach ($proof_list as $k=>$v){
                $proof_list[$k]['images'] = $v['images']?json_decode($v['images']):[];
                $proof_list[$k]['ls_dj_images'] = $v['ls_dj_images']?json_decode($v['ls_dj_images']):[];
                $proof_list[$k]['yj_images'] = $v['yj_images']?json_decode($v['yj_images']):[];

            }
        }

        //产品保险
        $insure_info =  Db::name('insure')
            ->where('product_id',$batch_info['product_id'])
            ->where('status',1)
            ->find();

        if($insure_info){
            $insure_info['images'] = $insure_info['images']?json_decode($insure_info['images']):[];
        }

        //生长过程
        $process_1_list =  Db::name('process')
            ->where('product_id',$batch_info['product_id'])
            ->where('status',1)
            ->where('type',1)
            ->order('sort asc,id asc')
            ->select()->toArray();

        //生产过程
        $process_2_list =  Db::name('process')
            ->where('product_id',$batch_info['product_id'])
            ->where('status',1)
            ->where('type',2)
            ->order('sort asc,id asc')
            ->select()->toArray();
        if($process_1_list){
            foreach ($process_1_list as $k=>$v){
                $process_1_list[$k]['image'] = $v['image']?json_decode($v['image']):[];
            }
        }
        if($process_2_list){
            foreach ($process_2_list as $k=>$v){
                $process_2_list[$k]['image'] = $v['image']?json_decode($v['image']):[];
            }
        }

        $landmark_fruit =  Db::name('landmark_fruit')
            ->where('product_id',$batch_info['product_id'])
            ->where('status',1)
            ->find();
        if($landmark_fruit){
            $landmark_fruit['protection_image'] = $landmark_fruit['protection_image']?json_decode($landmark_fruit['protection_image']):[];
            $landmark_fruit['image'] = $landmark_fruit['image']?json_decode($landmark_fruit['image']):[];
        }

        $landmark_protect =  Db::name('landmark_protect')
            ->where('product_id',$batch_info['product_id'])
            ->where('status',1)
            ->find();
        if($landmark_protect){
            $landmark_protect['protection_image'] = $landmark_protect['protection_image']?json_decode($landmark_protect['protection_image']):[];
        }
        $landmark_trademark =  Db::name('landmark_trademark')
            ->where('product_id',$batch_info['product_id'])
            ->where('status',1)
            ->find();
        if($landmark_trademark){
            $landmark_trademark['image'] = $landmark_trademark['image']?json_decode($landmark_trademark['image']):[];
        }
        $flavour =  Db::name('flavour')
            ->where('product_id',$batch_info['product_id'])
            ->where('status',1)
            ->find();
        if($flavour){
            $flavour['image'] = $flavour['image']?json_decode($flavour['image'],true):[];
        }

        $ec_data = [
            'traceability_info'=>['name'=>'溯源','data'=>$traceability_info],
            'product_info'=>['name'=>'产品信息','data'=>$product_info],
            'company_info'=>['name'=>'生产企业','data'=>$company_info],
            'batch_info'=>['name'=>'批次信息','data'=>$batch_info],
            'proof_list'=>['name'=>'检测检验','data'=>$proof_list],
            'insure_info'=>['name'=>'产品保险','data'=>$insure_info],
            'process_1_list'=>['name'=>'生长过程','data'=>$process_1_list],
            'process_2_list'=>['name'=>'生产过程','data'=>$process_2_list],
            'grade'=>['name'=>'产品分级','data'=>$grade_info],
            'landmark_fruit'=>['name'=>'原产地地理标志','data'=>$landmark_fruit],
            'landmark_protect'=>['name'=>'农产品地理保护','data'=>$landmark_protect],
            'landmark_trademark'=>['name'=>'农产品地理商标','data'=>$landmark_trademark],
            'flavour'=>['name'=>'特色原产地产品等级','data'=>$flavour],
            'code'=>$code,
            'message'=>'恭喜，您查询的产品是:'.$product_info['name'].' '.$product_info['specifications'].'该产品是正规产品，感谢您的选购。'
        ];
        $ec_data_1 = [
            'traceability_info'=>$traceability_info,
            'product_info'=>$product_info,
            'company_info'=>$company_info,
            'batch_info'=>$batch_info,
            'proof_list'=>$proof_list,
            'insure_info'=>$insure_info,
            'process_1_list'=>$process_1_list,
            'process_2_list'=>$process_2_list,
            'grade'=>$grade_info,
            'landmark_fruit'=>$landmark_fruit,
            'landmark_protect'=>$landmark_protect,
            'landmark_trademark'=>$landmark_trademark,
            'flavour'=>json_encode($flavour),
            'code'=>$code,
            'imgUrl'=>'http://localhost:8054/',
            'message'=>'恭喜，您查询的产品是:'.$product_info['name'].' '.$product_info['specifications'].'该产品是正规产品，感谢您的选购。'
        ];

        return View::fetch('index',$ec_data_1);
//        exit();
        return  json_encode(re_success('恭喜，您查询的产品是:'.$product_info['name'].' '.$product_info['specifications'].'该产品是正规产品，感谢您的选购。',$ec_data));
    }
    public function getC()
    {

        $traceability = 'traceability';
        $code = input('get.c');//oYRX3AO1Oysrv6lxJvYo7CICWq3wuCVc--//nGopaOchFZMrv6lxJvYo7CICWq3wuCVc
        $code = str_replace(' ','+',$code);
        $code = openssl_decrypt($code, 'DES-ECB', config('app')['cache_shaf'], 0);

        $traceability_info =  Db::name($traceability)
            ->where('code',$code)
            ->find();
        if(!$traceability_info){
            return  re_error('您查询的'.$code.'产品不存在');
            die();
        }

        //批次信息
        $batch_info =  Db::name('batch')
            ->where('wdm_open','<',$traceability_info['wdm'])
            ->where('wdm_end','>',$traceability_info['wdm'])
            ->whereOr('wdm_open','=',$traceability_info['wdm'])
            ->whereOr('wdm_end','=',$traceability_info['wdm'])
            ->find();

        if(!$batch_info){
            return  json_encode(re_error('空余的编码'));
            die();
        }

        //单位信息
        $company_info =  Db::name('company')
            ->where('id',$batch_info['company_id'])
            ->where('status',1)
            ->find();
        unset($company_info['password']);
        unset($company_info['key']);

        //产品信息
        $product_info =  Db::name('product')
            ->where('id',$batch_info['product_id'])
            ->find();
        if($product_info){
            $product_info['image'] = $product_info['image']?json_decode($product_info['image']):[];
            $product_info['images'] = $product_info['images']?json_decode($product_info['images']):[];
            $product_info['protection_image'] = $product_info['protection_image']?json_decode($product_info['protection_image']):[];

        }


        //批次等级
        $grade_info =  Db::name('grade')
            ->where('id',$batch_info['grade_id'])
            ->where('status',1)
            ->find();


        $jball = ['一星级','二星级','三星级','四星级','五星级'];

        if($grade_info){
            $grade_info['rank'] = $jball[intval($grade_info['rank'])-1];
            $grade_info['image'] = $grade_info['image']?json_decode($grade_info['image']):[];
        }

        //批次检测检验
        $proof_list =  Db::name('proof')
            ->where('batch_id',$batch_info['id'])
            ->where('status',1)
            ->select()->toArray();
        if($proof_list){
            foreach ($proof_list as $k=>$v){
                $proof_list[$k]['images'] = $v['images']?json_decode($v['images']):[];
                $proof_list[$k]['ls_dj_images'] = $v['ls_dj_images']?json_decode($v['ls_dj_images']):[];
                $proof_list[$k]['yj_images'] = $v['yj_images']?json_decode($v['yj_images']):[];

            }
        }

        //产品保险
        $insure_info =  Db::name('insure')
            ->where('product_id',$batch_info['product_id'])
            ->where('status',1)
            ->find();

        if($insure_info){
            $insure_info['images'] = $insure_info['images']?json_decode($insure_info['images']):[];
        }

        //生长过程
        $process_1_list =  Db::name('process')
            ->where('product_id',$batch_info['product_id'])
            ->where('status',1)
            ->where('type',1)
            ->order('sort asc,id asc')
            ->select()->toArray();

        //生产过程
        $process_2_list =  Db::name('process')
            ->where('product_id',$batch_info['product_id'])
            ->where('status',1)
            ->where('type',2)
            ->order('sort asc,id asc')
            ->select()->toArray();
        if($process_1_list){
            foreach ($process_1_list as $k=>$v){
                $process_1_list[$k]['image'] = $v['image']?json_decode($v['image']):[];
            }
        }
        if($process_2_list){
            foreach ($process_2_list as $k=>$v){
                $process_2_list[$k]['image'] = $v['image']?json_decode($v['image']):[];
            }
        }

        $landmark_fruit =  Db::name('landmark_fruit')
            ->where('product_id',$product_info['id'])
            ->where('status',1)
            ->find();
		
        if($landmark_fruit){
            $landmark_fruit['protection_image'] = $landmark_fruit['protection_image']?json_decode($landmark_fruit['protection_image']):[];
            $landmark_fruit['image'] = $landmark_fruit['image']?json_decode($landmark_fruit['image']):[];
        }

        $landmark_protect =  Db::name('landmark_protect')
            ->where('product_id',$product_info['id'])
            ->where('status',1)
            ->find();
        if($landmark_protect){
            $landmark_protect['image'] = $landmark_protect['image']?json_decode($landmark_protect['image']):[];
            $landmark_protect['protection_image'] = $landmark_protect['protection_image']?json_decode($landmark_protect['protection_image']):[];
        }
        $landmark_trademark =  Db::name('landmark_trademark')
            ->where('product_id',$product_info['id'])
            ->where('status',1)
            ->find();
        if($landmark_trademark){
            $landmark_trademark['image'] = $landmark_trademark['image']?json_decode($landmark_trademark['image']):[];
        }
        $flavour =  Db::name('flavour')
            ->where('product_id',$batch_info['product_id'])
            ->where('status',1)
            ->find();
        if($flavour){
            $flavour['image'] = $flavour['image']?json_decode($flavour['image'],true):[];
        }

        $ec_data = [
            'traceability_info'=>['name'=>'溯源','data'=>$traceability_info],
            'product_info'=>['name'=>'产品信息','data'=>$product_info],
            'company_info'=>['name'=>'生产企业','data'=>$company_info],
            'batch_info'=>['name'=>'批次信息','data'=>$batch_info],
            'proof_list'=>['name'=>'检测检验','data'=>$proof_list],
            'insure_info'=>['name'=>'产品保险','data'=>$insure_info],
            'process_1_list'=>['name'=>'生长过程','data'=>$process_1_list],
            'process_2_list'=>['name'=>'生产过程','data'=>$process_2_list],
            'grade'=>['name'=>'产品分级','data'=>$grade_info],
            'landmark_fruit'=>['name'=>'农产品地理标志','find'=>'landmark_fruit','data'=>$landmark_fruit],
            'landmark_protect'=>['name'=>'地理标志保护产品','find'=>'landmark_protect','data'=>$landmark_protect],
            'landmark_trademark'=>['name'=>'地理标志证明商标','find'=>'landmark_trademark','data'=>$landmark_trademark],
            'flavour'=>['name'=>'特色原产地产品等级','data'=>$flavour],
            'code'=>$code,
            'message'=>'恭喜，您查询的产品是:'.$product_info['name'].' '.$product_info['specifications'].'该产品是正规产品，感谢您的选购。'
        ];
        return  re_success('成功',$ec_data);
    }
    public function gettt(){
        return 1;
    }
    public function GetZgGi(){
        //溯源
//        $data = ['login' => '1521514763@qq.com', 'password' => '123456'];
//        $url = 'http://116.63.157.3:8069//api/user/login';

//        'username'=>'yunnansy',
//        'kenCode'=>'b3ef9aaa468545c49003c6ab27740f39',
//        'time'=>3600
        $data = ['username' => 'yunnansy', 'kenCode' => 'b3ef9aaa468545c49003c6ab27740f39'];
        $url = 'www.zg-gi.com/apis/Product/UserAuthorization';
        $curl = curl_init(); // 启动一个CURL会话
        curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址
        curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)'); // 模拟用户使用的浏览器
        curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data); // Post提交的数据包x
        curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制 防止死循环
        curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回

        $tmpInfo = curl_exec($curl); // 执行操作
        dump($curl);
        if(curl_errno($curl))
        {
            echo 'Errno'.curl_error($curl);//捕抓异常
        }
        curl_close($curl); // 关闭CURL会话
        return $tmpInfo; // 返回数据
    }
    public function login(){
        return 1232465776898900;
    }
    public  function post_url($url, $data)
    {
        $curl = curl_init(); // 启动一个CURL会话
        curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址
        curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)'); // 模拟用户使用的浏览器
        curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data); // Post提交的数据包x
        curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制 防止死循环
        curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
        $tmpInfo = curl_exec($curl); // 执行操作
        if(curl_errno($curl))
        {
            echo 'Errno'.curl_error($curl);//捕抓异常
        }
        curl_close($curl); // 关闭CURL会话
        return $tmpInfo; // 返回数据
    }

    function curlRequest($url, $params = array(), $is_post = false, $time_out = 10, $header=array())
    {
        $str_cookie = isset($ext_params['str_cookie']) ? $ext_params['str_cookie'] : '';$ch = curl_init();//初始化curl
        curl_setopt($ch, CURLOPT_URL, $url);//抓取指定网页
        curl_setopt($ch, CURLOPT_HEADER, 0);//设置是否返回response header
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
        //当需要通过curl_getinfo来获取发出请求的header信息时，该选项需要设置为true
        curl_setopt($ch, CURLINFO_HEADER_OUT, true);
        curl_setopt($ch, CURLOPT_TIMEOUT, $time_out);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $time_out);
        curl_setopt($ch, CURLOPT_POST, $is_post);
        if ($is_post) {
            curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
        }
        if ($str_cookie) {
            curl_setopt($ch, CURLOPT_COOKIE, $str_cookie);
        }
        if ($header) {
            curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        }
        $response = curl_exec($ch);
        dump($response);
        $request_header = curl_getinfo( $ch, CURLINFO_HEADER_OUT);
        curl_close($ch);
        return $response;
    }


    public function UserAuthorization(){
        $data = array('username' => $this->dlbz['username'], 'kenCode' => $this->dlbz['kenCode']);
        $postdata = http_build_query($data);
        dump($postdata);
        exit();
    }
}
